package com.bysj.sausys.service.impl;

import com.bysj.sausys.service.ISysFileService;
import com.bysj.sausys.util.FileUtil;
import com.bysj.sausys.util.PropertiesUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 文件上传Service接口实现
 *
 * @author WangZK
 */
@Service("sysFileService")
public class SysFileServiceImpl implements ISysFileService {

    /**
     * 上传社团相关文件
     *
     * @param assFile
     * @param path
     * @return
     */
    @Override
    public String uploadAssFile(MultipartFile assFile, String path) {
        File newFile = upload(assFile, path);

        // 获取文件的相对路径
        String url = "/" + PropertiesUtil.getProperty("file.prefix") + "/"
                + PropertiesUtil.getProperty("file.ass") + "/"
                + newFile.getName();

        return url;
    }

    /**
     * 上传社团年审文件接口
     *
     * @param assAnnFile
     * @param path
     * @return
     */
    @Override
    public String uploadAssAnnFile(MultipartFile assAnnFile, String path) {
        File newFile = upload(assAnnFile, path);

        // 获取文件的相对路径
        String url = "/" + PropertiesUtil.getProperty("file.prefix") + "/"
                + PropertiesUtil.getProperty("file.ann") + "/"
                + newFile.getName();

        return url;
    }

    /**
     * 上传下载资源文件
     *
     * @param dataDowFile
     * @param path
     * @return
     */
    @Override
    public String uploadDataDowFile(MultipartFile dataDowFile, String path) {

        File newFile = upload(dataDowFile, path);

        // 获取文件的相对路径
        String url = "/" + PropertiesUtil.getProperty("file.prefix") + "/"
                + PropertiesUtil.getProperty("file.resources") + "/"
                + newFile.getName();

        return url;
    }

    /**
     * 上传社团活动申请策划书
     *
     * @param actPlanFile
     * @param path
     * @return
     */
    @Override
    public String uploadActPlanFile(MultipartFile actPlanFile, String path) {
        File newFile = upload(actPlanFile, path);

        // 获取文件的相对路径
        String url = "/" + PropertiesUtil.getProperty("file.prefix") + "/"
                + PropertiesUtil.getProperty("file.actPlan") + "/"
                + newFile.getName();

        return url;
    }

    /**
     * 上传推文图片
     *
     * @param actArtFile
     * @param path
     * @return
     */
    @Override
    public String uploadActArtFile(MultipartFile actArtFile, String path) {
        File newFile = upload(actArtFile, path);

        // 获取文件的相对路径
        String url = "/" + PropertiesUtil.getProperty("file.prefix") + "/"
                + PropertiesUtil.getProperty("file.art") + "/"
                + newFile.getName();

        return url;
    }

    /**
     * 文件上传
     *
     * @param file
     * @param path
     * @return
     */
    private File upload(MultipartFile file, String path) {

        // 创建文件夹
        FileUtil.createFolder(path);

        // 生成文件名称
        String originalFilename = file.getOriginalFilename();
        String newFilename = FileUtil.getNewFileName(originalFilename);

        // 创建新文件
        File newFile = new File(path, newFilename);

        // 复制文件
        FileUtil.copyFile(file, newFile);

        return newFile;
    }
}
